package cn.wolfcode.luowowo.controller;


import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Region;
import cn.wolfcode.luowowo.query.RegionQuery;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.IRegionService;
import cn.wolfcode.luowowo.util.JsonResult;
import com.sun.xml.internal.bind.v2.model.core.RegistryInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/region")
public class RegionController {

    @Autowired
    private IRegionService regionService;
    @Autowired
    private IDestinationService destinationService;

    //区域界面
    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo") RegionQuery qo){
        //使用mongodb的分页插件
        Page<Region> page = regionService.query(qo);
        model.addAttribute("page",page);
        return "region/list";
    }

    //添加操作
    @GetMapping("/getDestByDeep")
    @ResponseBody
    public Object getDestByDeep(int deep){
        //查询出目的地返回
        List<Destination> list = destinationService.list();
        return list;
    }

    //添加保存操作
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Region region){
        regionService.saveOrUpdate(region);
        return JsonResult.success();
    }

    //查看功能
    @RequestMapping("/getDestByRegionId")
    @ResponseBody
    public Object getDestByRegionId(String rid){
       //获取目的地的名称
        List<Destination> destinations = destinationService.getDestByRegionId(rid);
        return destinations;
    }

    //实现删除功能
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){
        regionService.delete(id);
        return JsonResult.success();
    }
    //实现设置热门 ,取消热门
    @RequestMapping("/changeHotValue")
    @ResponseBody
    public Object changeHotValue(String id,int hot){
        Region region = regionService.get(id);
        //更改热门的属性
        region.setIshot(hot);
        //重新保存
        regionService.update(region);
        return JsonResult.success();
    }
    //模拟git
    @RequestMapping("/change1111")
    @ResponseBody
    public Object change(String id,int hot){
        Region region = regionService.get(id);
        //更改热门的属性
        region.setIshot(hot);
        //重新保存
        regionService.update(region);
        return JsonResult.success();
    }

}
